'use strict';


angular.module('core')
    .controller('SideMenuController', ['$scope', '$rootScope', '$state', '$location', '$filter', 'User', 'Initialization', 'BaseModule', 'BaseEnum',
        function ($scope, $rootScope, $state, $location, $filter, User, Initialization, BaseModule, BaseEnum) {
            $scope.data = {
                navMenuList: [],
                navSubMenu: [],
                subMenuVisible: false,
                activeMenu: 'home',
                activeSubMenu: ''
            };

            var childrenMenu = {};

            Initialization.initPromise.then(init);    

            $scope.goHome = function () {
                $state.go('index.home');
                $scope.data.activeMenu = 'home';
                $scope.data.navSubMenu = [];
                $scope.data.subMenuVisible = false;
                $scope.data.activeSubMenu = '';
            };
            $scope.clickMenu = function (menuId) {
                if (childrenMenu.hasOwnProperty(menuId)) {
                    $scope.data.activeMenu = menuId;
                    $scope.data.navSubMenu = childrenMenu[menuId];
                    $scope.data.subMenuVisible = true;
                }
            };
            $scope.clickSubMenu = function (menu) {
                $scope.data.subMenuVisible = false;
                $scope.data.activeSubMenu = menu.id;
                $location.url('/index' + menu.URL);

                $rootScope.$broadcast('subMenuChange', menu);
            };

            $scope.$on('subMenuChange', function (e, menu) {
                if (menu && menu.notRolesMenu) {
                    $scope.data.activeMenu = '';
                    $scope.data.activeSubMenu = '';
                }
            });

            function init () {
                var profile = User.getProfile();
                var roles = profile.Roles ? profile.Roles : null;

                BaseModule.initModule($scope, 'Menu');
                $scope.Module.Menu.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
                $scope.Module.Menu.Populate = {path: 'Parent', select: {Name: 1, Code: 1}};

                BaseModule.initModule($scope, 'Role');
                $scope.Module.Role.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
                $scope.Module.Role.load({_id: {$in: roles}, IsEffective: true}, null, function(){
                    var menuIds = [];
                    var records = $scope.Module.Role.Records;
                    for(var i=0; i<records.length; i++){
                        menuIds = menuIds.concat(records[i].Menus);
                    }

                    $scope.Module.Menu.load({_id: {$in: menuIds}}, null, function(){
                        var mainMenu = [],
                            currentURL = $state.current.url,
                            valid = false,
                            subMenu;
                        if ($state.current.access && $state.current.access.notRequiredRoles) {
                            valid = true;
                            subMenu = {
                                Name: $state.current.access.name
                            };
                        }
                        var menuRecords = $scope.Module.Menu.Records;
                        angular.forEach(menuRecords, function (menu) {
                            if (menu.Parent != undefined && menu.Parent != null) {
                                if(!childrenMenu.hasOwnProperty(menu.Parent._id)){
                                    mainMenu.push(menu.Parent);
                                    childrenMenu[menu.Parent._id] = [];
                                }

                                childrenMenu[menu.Parent._id].push({
                                    id: menu._id,
                                    Code: menu.Code,
                                    Name: menu.Name,
                                    URL: menu.URL
                                });
                                if (!valid && currentURL.indexOf(menu.URL) != -1) {
                                    valid = true;
                                    subMenu = menu;
                                    $scope.data.activeMenu = menu.Parent._id;
                                    $scope.data.activeSubMenu = menu._id;
                                }
                            }                            
                        });
                        $scope.data.navMenuList = $filter('orderBy')(mainMenu, 'Code', false);
                        angular.forEach(childrenMenu, function (value, key) {
                            childrenMenu[key] = $filter('orderBy')(value, 'Code', false);
                        });
                        if (!valid) {
                            $state.go('index.home');
                        }
                        $rootScope.$broadcast('subMenuChange', subMenu);
                    });
                });
            }
        }
    ]);